﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace HiViLicenseManage
{
	public partial class ManagerUserMember : System.Web.UI.Page
	{
		DataTable dt;
		protected void Page_Load(object sender, EventArgs e)
		{
			LoadUserMember(txtSearch.Text.Trim(), drSort.SelectedValue.ToString());
		}

		private void LoadUserMember(string filter, string sort)
		{
			SqlConnection appConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["appconn"].ConnectionString);
			appConn.Open();
			if (filter == "")
			{
				SqlCommand sqlComm;
				if (ckNotActive.Checked)
					sqlComm = new SqlCommand("SELECT * FROM Agent WHERE Status = 'False' ORDER BY " + sort + " DESC", appConn);
				else
					sqlComm = new SqlCommand("SELECT * FROM Agent ORDER BY " + sort + " DESC", appConn);
				sqlComm.CommandType = CommandType.Text;
				SqlDataAdapter sqlDA = new SqlDataAdapter();
				sqlDA.SelectCommand = sqlComm;
				dt = new DataTable();
				sqlDA.Fill(dt);

				gdvMember.DataSource = dt;
				gdvMember.DataBind();
			}
			else
			{
				SqlCommand sqlComm;
				if (ckNotActive.Checked)
					sqlComm = new SqlCommand("SELECT * FROM Agent WHERE (AgentID like N'%" + filter + "%' OR AgentName like N'%" + filter + "%' OR Company like N'%" + filter + "%' OR Email like '%" + filter + "%') AND Status = 'False' ORDER BY " + sort + " DESC", appConn);
				else
					sqlComm = new SqlCommand("SELECT * FROM Agent WHERE AgentID like N'%" + filter + "%' OR AgentName like N'%" + filter + "%' OR Company like N'%" + filter + "%' OR Email like '%" + filter + "%' ORDER BY " + sort + " DESC", appConn);

				sqlComm.CommandType = CommandType.Text;
				SqlDataAdapter sqlDA = new SqlDataAdapter();
				sqlDA.SelectCommand = sqlComm;
				dt = new DataTable();
				sqlDA.Fill(dt);

				gdvMember.DataSource = dt;
				gdvMember.DataBind();
			}
		}

		protected void btSearch_Click(object sender, EventArgs e)
		{
			LoadUserMember(txtSearch.Text.Trim(), drSort.SelectedValue.ToString());
		}

		protected void drSort_SelectedIndexChanged(object sender, EventArgs e)
		{
			LoadUserMember(txtSearch.Text.Trim(), drSort.SelectedValue.ToString());
		}

		protected void btnLink_Click(object sender, EventArgs e)
		{
			LinkButton lb = (LinkButton)sender;
			Session["UserMember"] = lb.Text;
			Response.Redirect("~/ChangeUserMember.aspx", false);
		}

		protected void gdvMember_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{
			gdvMember.PageIndex = e.NewPageIndex;
			gdvMember.DataBind();
		}
	}
}
